<?php namespace Controller;

abstract class Controller {

	protected $models = array();
	protected $params = array();
	protected $action = array();
	protected $form = array();
	protected $files = null;
	protected $viewToRender = null;
	protected $viewData = array();

	public function invokeAction($action, $params, $form, $files) {
		if(method_exists($this, $action)) {
			$this->action = $action;
			$this->viewToRender = $this->action;
			$this->params = $params;
			$this->form = $form;
			$this->files = $files;
			$this->$action()
				 ->_invokeView(substr(get_called_class(),11), $this->viewToRender, $this->viewData)
			     ->render();
		}
	}

	protected function _set($key,$value) {
		$this->viewData[$key] = $value;
	}

	protected function _invokeView($controller, $viewName, $data) {
		return new \View($controller, $viewName, $data);
	}

	protected function _loadModels() {
		$this->models = array_combine($this->models, array_map(function($mod) {
			$mod = 'Model\\'.$mod; return new $mod;
		}, $this->models));
		$db = new \Database;
		foreach ($this->models as $key => $value) {
			$value->setDB($db);
		}
	}
}